<!DOCTYPE HTML>
<html lang="zh-tw" >
    <!-- Start book python基础语言 -->
    <head>
        <!-- head:start -->
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>私有化 | python基础语言</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-splitter/splitter.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-emphasize/plugin.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="../01day/section.3.1.html" />
    
    
    <link rel="prev" href="../03day/section.6.html" />
    

        <!-- head:end -->
    </head>
    <body>
        <!-- body:start -->
        
    <div class="book"
        data-level="2.7"
        data-chapter-title="私有化"
        data-filepath="01day/section.3.md"
        data-basepath=".."
        data-revision="Fri Mar 17 2017 08:48:09 GMT+0800 (CST)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        课程介绍
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="01day/index.html">
            
                
                    <a href="../01day/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        面向对象进阶
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1" data-path="01day/section.1.0.html">
            
                
                    <a href="../01day/section.1.0.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.</b>
                        
                        元类
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="01day/section.1.1.html">
            
                
                    <a href="../01day/section.1.1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.</b>
                        
                        python是动态语言
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="01day/section.1.3.html">
            
                
                    <a href="../01day/section.1.3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.3.</b>
                        
                        __slots__
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="02day/section.4.html">
            
                
                    <a href="../02day/section.4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.4.</b>
                        
                        生成器
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="02day/section.3.html">
            
                
                    <a href="../02day/section.3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.5.</b>
                        
                        迭代器
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="02day/section.1.html">
            
                
                    <a href="../02day/section.1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.6.</b>
                        
                        闭包
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="02day/section.2.html">
            
                
                    <a href="../02day/section.2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.7.</b>
                        
                        装饰器
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2" data-path="02day/index.html">
            
                
                    <a href="../02day/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        其他的知识点
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="03day/section.0.2.html">
            
                
                    <a href="../03day/section.0.2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        import导入模块
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="03day/section.1.1.html">
            
                
                    <a href="../03day/section.1.1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        循环导入
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="03day/section.0.3.html">
            
                
                    <a href="../03day/section.0.3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.</b>
                        
                        作用域
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="03day/section.2.html">
            
                
                    <a href="../03day/section.2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.4.</b>
                        
                        ==、is
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="03day/section.1.html">
            
                
                    <a href="../03day/section.1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.5.</b>
                        
                        深拷贝、浅拷贝
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.6" data-path="03day/section.6.html">
            
                
                    <a href="../03day/section.6.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.6.</b>
                        
                        进制、位运算
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="2.7" data-path="01day/section.3.html">
            
                
                    <a href="../01day/section.3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.7.</b>
                        
                        私有化
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.8" data-path="01day/section.3.1.html">
            
                
                    <a href="../01day/section.3.1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.8.</b>
                        
                        属性property
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3" data-path="03day/index.html">
            
                
                    <a href="../03day/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        其他的知识点
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1" data-path="03day/section.0.1.html">
            
                
                    <a href="../03day/section.0.1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                        垃圾回收(一)
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="03day/section.0.1.1.html">
            
                
                    <a href="../03day/section.0.1.1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                        垃圾回收(二)
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="03day/section.0.1.2.html">
            
                
                    <a href="../03day/section.0.1.2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.</b>
                        
                        垃圾回收(三)-gc模块
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="01day/section.4.html">
            
                
                    <a href="../01day/section.4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.4.</b>
                        
                        内建属性
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.5" data-path="01day/section.6.html">
            
                
                    <a href="../01day/section.6.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.5.</b>
                        
                        内建函数
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.6" data-path="01day/section.7.html">
            
                
                    <a href="../01day/section.7.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.6.</b>
                        
                        集合set
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.7" data-path="02day/section.5.html">
            
                
                    <a href="../02day/section.5.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.7.</b>
                        
                        functools
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.8" data-path="03day/section.4.html">
            
                
                    <a href="../03day/section.4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.8.</b>
                        
                        模块
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.9" data-path="03day/section.3.html">
            
                
                    <a href="../03day/section.3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.9.</b>
                        
                        调试
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.10" data-path="03day/section.5.html">
            
                
                    <a href="../03day/section.5.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.10.</b>
                        
                        编码风格
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    本書使用 GitBook 釋出
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../" >python基础语言</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="&#x79C1;&#x6709;&#x5316;">&#x79C1;&#x6709;&#x5316;</h1>
<ul>
<li>xx: &#x516C;&#x6709;&#x53D8;&#x91CF;</li>
<li>_x: &#x5355;&#x524D;&#x7F6E;&#x4E0B;&#x5212;&#x7EBF;,&#x79C1;&#x6709;&#x5316;&#x5C5E;&#x6027;&#x6216;&#x65B9;&#x6CD5;&#xFF0C;from somemodule import *&#x7981;&#x6B62;&#x5BFC;&#x5165;,&#x7C7B;&#x5BF9;&#x8C61;&#x548C;&#x5B50;&#x7C7B;&#x53EF;&#x4EE5;&#x8BBF;&#x95EE;</li>
<li>__xx&#xFF1A;&#x53CC;&#x524D;&#x7F6E;&#x4E0B;&#x5212;&#x7EBF;,&#x907F;&#x514D;&#x4E0E;&#x5B50;&#x7C7B;&#x4E2D;&#x7684;&#x5C5E;&#x6027;&#x547D;&#x540D;&#x51B2;&#x7A81;&#xFF0C;&#x65E0;&#x6CD5;&#x5728;&#x5916;&#x90E8;&#x76F4;&#x63A5;&#x8BBF;&#x95EE;(&#x540D;&#x5B57;&#x91CD;&#x6574;&#x6240;&#x4EE5;&#x8BBF;&#x95EE;&#x4E0D;&#x5230;)</li>
<li>__xx__:&#x53CC;&#x524D;&#x540E;&#x4E0B;&#x5212;&#x7EBF;,&#x7528;&#x6237;&#x540D;&#x5B57;&#x7A7A;&#x95F4;&#x7684;&#x9B54;&#x6CD5;&#x5BF9;&#x8C61;&#x6216;&#x5C5E;&#x6027;&#x3002;&#x4F8B;&#x5982;:<code>__init__</code> , __
&#x4E0D;&#x8981;&#x81EA;&#x5DF1;&#x53D1;&#x660E;&#x8FD9;&#x6837;&#x7684;&#x540D;&#x5B57;</li>
<li>xx_:&#x5355;&#x540E;&#x7F6E;&#x4E0B;&#x5212;&#x7EBF;,&#x7528;&#x4E8E;&#x907F;&#x514D;&#x4E0E;Python&#x5173;&#x952E;&#x8BCD;&#x7684;&#x51B2;&#x7A81;</li>
</ul>
<p>&#x901A;&#x8FC7;name mangling&#xFF08;&#x540D;&#x5B57;&#x91CD;&#x6574;(&#x76EE;&#x7684;&#x5C31;&#x662F;&#x4EE5;&#x9632;&#x5B50;&#x7C7B;&#x610F;&#x5916;&#x91CD;&#x5199;&#x57FA;&#x7C7B;&#x7684;&#x65B9;&#x6CD5;&#x6216;&#x8005;&#x5C5E;&#x6027;)&#x5982;&#xFF1A;_Class__object&#xFF09;&#x673A;&#x5236;&#x5C31;&#x53EF;&#x4EE5;&#x8BBF;&#x95EE;private&#x4E86;&#x3002;</p>
<pre><code class="lang-python"><span class="hljs-comment">#coding=utf-8</span>

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Person</span><span class="hljs-params">(object)</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self, name, age, taste)</span>:</span>
        self.name = name
        self._age = age 
        self.__taste = taste

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">showperson</span><span class="hljs-params">(self)</span>:</span>
        print(self.name)
        print(self._age)
        print(self.__taste)

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">dowork</span><span class="hljs-params">(self)</span>:</span>
        self._work()
        self.__away()


    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">_work</span><span class="hljs-params">(self)</span>:</span>
        print(<span class="hljs-string">&apos;my _work&apos;</span>)

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__away</span><span class="hljs-params">(self)</span>:</span>
        print(<span class="hljs-string">&apos;my __away&apos;</span>)

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Student</span><span class="hljs-params">(Person)</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">construction</span><span class="hljs-params">(self, name, age, taste)</span>:</span>
        self.name = name
        self._age = age 
        self.__taste = taste

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">showstudent</span><span class="hljs-params">(self)</span>:</span>
        print(self.name)
        print(self._age)
        print(self.__taste)

<span class="hljs-decorator">    @staticmethod</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">testbug</span><span class="hljs-params">()</span>:</span>
        _Bug.showbug()

<span class="hljs-comment">#&#x6A21;&#x5757;&#x5185;&#x53EF;&#x4EE5;&#x8BBF;&#x95EE;&#xFF0C;&#x5F53;from  cur_module import *&#x65F6;&#xFF0C;&#x4E0D;&#x5BFC;&#x5165;</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">_Bug</span><span class="hljs-params">(object)</span>:</span>
<span class="hljs-decorator">    @staticmethod</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">showbug</span><span class="hljs-params">()</span>:</span>
        print(<span class="hljs-string">&quot;showbug&quot;</span>)

s1 = Student(<span class="hljs-string">&apos;jack&apos;</span>, <span class="hljs-number">25</span>, <span class="hljs-string">&apos;football&apos;</span>)
s1.showperson()
print(<span class="hljs-string">&apos;*&apos;</span>*<span class="hljs-number">20</span>)

<span class="hljs-comment">#&#x65E0;&#x6CD5;&#x8BBF;&#x95EE;__taste,&#x5BFC;&#x81F4;&#x62A5;&#x9519;</span>
<span class="hljs-comment">#s1.showstudent() </span>
s1.construction(<span class="hljs-string">&apos;rose&apos;</span>, <span class="hljs-number">30</span>, <span class="hljs-string">&apos;basketball&apos;</span>)
s1.showperson()
print(<span class="hljs-string">&apos;*&apos;</span>*<span class="hljs-number">20</span>)

s1.showstudent()
print(<span class="hljs-string">&apos;*&apos;</span>*<span class="hljs-number">20</span>)

Student.testbug()
</code></pre>
<p><img src="../Images/private.png" alt="&#x79C1;&#x6709;&#x53D8;&#x91CF;"></p>
<h4 id="&#x603B;&#x7ED3;">&#x603B;&#x7ED3;</h4>
<ul>
<li>&#x7236;&#x7C7B;&#x4E2D;&#x5C5E;&#x6027;&#x540D;&#x4E3A;<code>__&#x540D;&#x5B57;</code>&#x7684;&#xFF0C;&#x5B50;&#x7C7B;&#x4E0D;&#x7EE7;&#x627F;&#xFF0C;&#x5B50;&#x7C7B;&#x4E0D;&#x80FD;&#x8BBF;&#x95EE;</li>
<li>&#x5982;&#x679C;&#x5728;&#x5B50;&#x7C7B;&#x4E2D;&#x5411;<code>__&#x540D;&#x5B57;</code>&#x8D4B;&#x503C;&#xFF0C;&#x90A3;&#x4E48;&#x4F1A;&#x5728;&#x5B50;&#x7C7B;&#x4E2D;&#x5B9A;&#x4E49;&#x7684;&#x4E00;&#x4E2A;&#x4E0E;&#x7236;&#x7C7B;&#x76F8;&#x540C;&#x540D;&#x5B57;&#x7684;&#x5C5E;&#x6027;</li>
<li><code>_&#x540D;</code>&#x7684;&#x53D8;&#x91CF;&#x3001;&#x51FD;&#x6570;&#x3001;&#x7C7B;&#x5728;&#x4F7F;&#x7528;<code>from xxx import *</code>&#x65F6;&#x90FD;&#x4E0D;&#x4F1A;&#x88AB;&#x5BFC;&#x5165;</li>
</ul>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../03day/section.6.html" class="navigation navigation-prev " aria-label="Previous page: 进制、位运算"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../01day/section.3.1.html" class="navigation navigation-next " aria-label="Next page: 属性property"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../gitbook/app.js"></script>

    
    <script src="../gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-splitter/splitter.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-livereload/plugin.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"toggle-chapters":{},"splitter":{},"emphasize":{},"highlight":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"livereload":{}};
    gitbook.start(config);
});
</script>

        <!-- body:end -->
    </body>
    <!-- End of book python基础语言 -->
</html>
